<?php


namespace app\wenchuang\controller;


use app\wenchuang\model\TheoryModel;
use app\wenchuang\validate\TheoryValidate;
use cmf\controller\AdminBaseController;

class TheoryController  extends AdminBaseController
{
      //显示列表
      public function index()
      {
          $title = $this->request->param('title');
          $startTime = $this->request->param('start/s');
          $endTime = $this->request->param('end/s');
          $status = $this->request->param('status/d');
          $where = [];
          if ($title) {
              $where[] = ['title', 'like', '%'.$title.'%'];
          }
          if (!empty($startTime) && empty($endTime)) {
              $where[] = ['create_time', '>=', strtotime($startTime)];
          }
          if (empty($startTime) && !empty($endTime)) {
              $where[] = ['create_time', '<=', strtotime($endTime)];
          }
          if (!empty($startTime) && !empty($endTime)) {
              $where[] = ['create_time', 'between time', [strtotime($startTime), strtotime($endTime)]];
          }
          if ($status != '') {
              $where[] = ['status', '=', $status];
          }
          $data = TheoryModel::order(['list_order' => 'desc'])
              ->where($where)
              ->paginate(10);
          // 追加请求参数，分页后仍然保留搜索条件
          $data->appends($this->request->param());
          // 得到分页按钮
          $page = $data->render();
          $this->assign('theory', $data);
          $this->assign('page', $page);
          return $this->fetch();
      }

      //排序
      public function listOrder()
      {
        parent::listOrders(new TheoryModel());
        $this->success('排序成功');
      }

      //显示添加页面
      public function add()
      {
          return $this->fetch();
      }

      //保存添加
      public function addpost()
      {
          $data = $this->request->param();

          $content = htmlspecialchars_decode($data['content']);
          $content = cmf_replace_content_file_url($content, true);
          $content = htmlspecialchars($content);
          $data['content'] = $content;
          $validate = new TheoryValidate();
          $result = $validate->scene('add')->check($data);
          if ($result !== true) {
              $this->error($validate->getError());
          }
          // 存库
          $TheoryModel = New TheoryModel();
          if ($TheoryModel->save($data)) {
              $this->success('添加成功', 'Theory/index');
          }
          $this->error('添加失败');
      }

      //显示修改页面
      public function edit()
      {
          $id = $this->request->param('id');
          $Theory = TheoryModel::find($id);
          //显示编辑页面也要处理存在数据库的富文本
          $Theory['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($Theory['content']));
          $this->assign('theory', $Theory);
          return $this->fetch();
      }

      //保存修改
      public function editPost()
      {
          $data = $this->request->param();
          $content = htmlspecialchars_decode($data['content']);
          $content = cmf_replace_content_file_url($content, true);
          $content = htmlspecialchars($content);
          $data['content'] = $content;
          // 验证数据
          $validate = new TheoryValidate();
          $result = $validate->scene('add')->check($data);
          if ($result !== true) {
              $this->error($validate->getError());
          }

          // 编辑存库
          $TheoryModel = New TheoryModel();
          $Theory= $TheoryModel->where('id', '=', $data['id'])->find();
          $Theory->title = $data['title'];
          $Theory->abstract = $data['abstract'];
          $Theory->image = $data['image'];
          $Theory->content = $data['content'];
          $Theory->author = $data['author'];
          if ($Theory->save()) {
              $this->success('保存成功', 'Theory/index');
          }
          $this->error('保存失败');
      }

      //删除
      public function softDelete()
      {
          $id = input('id');
          $TheoryModel = New TheoryModel();
          // 查询要删除的数据
          $Theory= $TheoryModel->where('id', '=', $id)->find();
          if ($Theory->delete()) {
              $this->success('删除成功');
          }
          $this->error('删除失败');
      }
}